﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.PaymentVoucher;
using SCMS.UI.Models;
using SCMS.CoreBusinessLogic.Request4Payment;
using SCMS.Resource;

namespace SCMS.UI.Controllers
{
    [MyException]
    public class PaymentVoucherController : PortalBaseController
    {
        private IPaymentVoucherService pVService;
        private IRequest4PaymentService RFPService;

        public PaymentVoucherController(IPermissionService permissionService, IUserContext userContext, IPaymentVoucherService pVServiceService,
            IRequest4PaymentService RFPService)
            : base(userContext, permissionService)
        {
            this.pVService = pVServiceService;
            this.RFPService = RFPService;
        }
        //
        // GET: /PaymentVoucher/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LoadPV()
        {
            pVoucher model = new pVoucher();
            model.EntityPV = new Model.PaymentVoucher();
            model.RFPEntity = new Model.PaymentRequest();
            model.RFPList = new SelectList(RFPService.GetPostedPaymentRequests(countryProg.Id), "Id", "RefNumber");
            model.EntityPV.RefNumber = string.Format("--{0}--", Resources_morgan.PaymentVoucher_LoadPV_NewPV);
            model.RFPBLz = new List<RFPBLView>();
            return View(model);
        }

        public ActionResult LoadRFPItemDetais(Guid RFPId)
        {
            pVoucher model = new pVoucher();
            model.EntityPV = new Model.PaymentVoucher();
            model.RFPList = new SelectList(RFPService.GetPostedPaymentRequests(countryProg.Id), "Id", "RefNumber");
            model.RFPEntity = RFPService.GetRFPById(RFPId);
            model.EntityPV.RefNumber = string.Format("--{0}--", Resources_morgan.PaymentVoucher_LoadPV_NewPV);
            model.RFPBLz = pVService.GetRFPItemsDetails(RFPId);
            model.EntityPV.PaymentRequestId = RFPId;
            return View("LoadPV", model);
        }

        public ActionResult EditPVDetais(Guid PVId)
        {
            pVoucher model = new pVoucher();
            Model.PaymentVoucher pv = pVService.GetPaymentVoucherById(PVId);
            model.EntityPV = pv;
            model.RFPEntity = pv.PaymentRequest;
            model.RFPList = new SelectList(RFPService.GetPostedPaymentRequests(countryProg.Id), "Id", "RefNumber");
            model.RFPBLz = pVService.GetPVItemsDetails(PVId);
            return View(model);
        }
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SavePV(Models.pVoucher entity)
        {
            entity.EntityPV.Id = Guid.NewGuid();
            entity.EntityPV.CountryProgrammeId = countryProg.Id;
            entity.EntityPV.PreparedBy = currentStaff.Id;
            entity.EntityPV.PreparedOn = DateTime.Now;
            entity.EntityPV.IsSubmitted = true;
            entity.EntityPV.RefNumber = pVService.GenerateUniquNumber(countryProg);
            entity.EntityPV.TotalAmount = entity.RFPBLz.Sum(p => p.amunt);
            if (!pVService.IsPaymentVoucherSaved(entity.EntityPV, entity.RFPBLz))
            {
                ViewBag.Response = 0;
                ViewBag.msg = Resources.Global_String_AnErrorOccurred;
                return LoadRFPItemDetais(entity.RFPEntity.Id);
            }
            //notificationService.SendToAppropriateApprover(NotificationHelper.grnCode, NotificationHelper.verificationCode, grn.Id);
            ViewBag.Response = 1;
            ViewBag.msg = Resources.Global_String_ProcessCompleted;
            return ViewPV();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditPVDetails(Models.pVoucher entity)
        {
            if (!pVService.IsPaymentVoucherUpdated(entity.EntityPV, entity.RFPBLz))
            {
                ViewBag.Response = 0;
                ViewBag.msg = Resources.Global_String_AnErrorOccurred;
                return EditPVDetais(entity.EntityPV.Id);
            }
            ViewBag.Response = 1;
            ViewBag.msg = Resources.Global_String_ProcessCompleted;
            return ViewPV();
        }

        public ActionResult DeletePV(Guid PVId)
        {
            pVService.IsPaymentVoucherDeleted(PVId);
            return ViewPV();
        }

        public ActionResult ViewPV()
        {
            return View("ViewPV", pVService.GetPaymentVouchers(countryProg.Id));
        }

        public ActionResult ViewPVDetails(Guid PVId)
        {
            return View(pVService.GetPaymentVoucherById(PVId));
        }
    }
}
